package bingo.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import net.bingosoft.common.ObjectRegistry;
import net.bingosoft.common.struts.ActionContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Mar 10, 2009 5:09:54 AM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:
 * ChangeLog:
 */
public class BaseAction {
	protected final Log log = LogFactory.getLog(this.getClass());
	private Map<String, String> viewMapping = new HashMap<String, String>();

	public Map<String, String> getViewMapping() {
		return viewMapping;
	}

	public void setViewMapping(Map<String, String> viewMapping) {
		this.viewMapping = viewMapping;
	}
	
	
	public void forwardToView(String viewKey) throws ServletException, IOException{
		ActionContext.forward(viewMapping.get(viewKey));
	}
	public void redirectToView(String viewKey) throws ServletException, IOException{
		ActionContext.redirect(viewMapping.get(viewKey));
	}
	
	/**
	 * 获取bean
	 * @param beanID
	 * @return
	 */
	protected <T>T getBean(String beanID,Class<T> type){
		return ObjectRegistry.getBean(beanID,type);
	}
 
    /**
     * 给链接URL添加参数
     * @param URL
     * @param paramName
     * @param paramValue
     * @return
     */
	protected static String appendParamToURI(String URL,String paramName,String paramValue){
    	String appendedURI = URL;
    	if(null != appendedURI){
    		if(appendedURI.indexOf("?") < 0){
    			appendedURI += "?";
    		}else{
    			appendedURI += "&";
    		}
        	appendedURI += ( paramName + "=" + paramValue);    		
    	}
    	
    	return appendedURI;
    }
}
